import lookup from "./lookup"

/**
 * 
 * @param {Array} tokens 
 * @param {object} data 
 * @returns 
 */
export default function renderTemplate(tokens,data){
    let resultStr = ''
    for(let token of tokens){
        if(token[0] === 'text'){
            resultStr += token[1]
        }else if(token[0] === 'name'){
            resultStr += lookup(data,token[1])
        }else if(token[0] === '#'){
            resultStr += parseArray(token,lookup(data,token[1]))
        }
    }
    return resultStr
}

function parseArray(token,data){
    let str = ''
    for(let i of data){
        str += renderTemplate(token[2],{...i,'.':i})
    }
    return str
}